package com.estar.app.business.mobileLogin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.estar.platform.caseInfo.entity.TmblSurveyInfoEntity;


@Controller
@RequestMapping("/taskReportNoTimer")
@Scope("prototype")
public class TaskReportNoTimer {

	private static int seq = 1;  
	private static String preStr = "0000";
	private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
	
	public void refreshNum(){
		try {
			seq = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static String createRptNo(CommonService commService){
		
		if(1 == seq){
			
			CriteriaQuery cq = new CriteriaQuery(TmblSurveyInfoEntity.class);
			cq.addOrder("createTm", SortDirection.desc);
			cq.add();
			List<TmblSurveyInfoEntity> surveyList = commService.getListByCriteriaQuery(cq, false);
			if(surveyList.size() > 0){
				TmblSurveyInfoEntity survey = surveyList.get(0);
				String rptNo = survey.getReportNo();
				String numStr = rptNo.substring(rptNo.length()-4, rptNo.length());
				String beforeTime = df.format(survey.getCreateTm());
				String nowTime = df.format(new Date());
				System.out.println(beforeTime + "--" + nowTime);
				
				if(nowTime.equals(beforeTime)){
					seq = Integer.valueOf(numStr) + 1;
				}
			}
			
			
		}
		
		String num = preStr + String.valueOf(seq);
		String seqNo = "KCLP" + df.format(new Date()) + num.substring(num.length()-4, num.length());
		System.out.println(seqNo);
		seq = seq + 1;
		return seqNo;
	}
}
